package com.leonne.harpacristabrasileira.tools;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;

import android.content.Context;
import android.content.res.Configuration;
import android.view.Display;
import android.view.WindowManager;

public class Utils {
	
	public static boolean isNumeric(String input) {
		try {
			Integer.parseInt(input);
			return true;
		} catch (NumberFormatException e) {
			// s is not numeric
			return false;
		}
	}
	
	public static String formatMiliseconds(int milliseconds) {
		int minutes = (milliseconds % (1000*60*60)) / (1000*60);
		int seconds = ((milliseconds % (1000*60*60)) % (1000*60)) / 1000;
		String strMins = (minutes < 10) ? "0" + String.valueOf(minutes) : String.valueOf(minutes);
		String strSecs = (seconds < 10) ? "0" + String.valueOf(seconds) : String.valueOf(seconds);
		return strMins + ":" + strSecs;
	}
	
	/**
	 * Detect if the device running the app is a tablet or not. Method extracted from:
	 * http://www.androidsnippets.com/how-to-detect-tablet-device
	 * 
	 * @param c The context of the activity
	 * @return True if is a tablet, otherwise false
	 */
	
	public static boolean isTabletDevice(Context c) {
	    if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
	        // test screen size, use reflection because isLayoutSizeAtLeast is only available since 11
	        Configuration con = c.getResources().getConfiguration();
	        try {
	            Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class);
	            Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
	            return r;
	        } catch (Exception x) {
	            x.printStackTrace();
	            return false;
	        }
	    }
	    return false;
	}
	
	public static boolean isWideScreen(Context c){
		WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		float width = display.getWidth();
		float height = display.getHeight();
		float proportion = height / width;
		return (proportion > 1.7);
	}
	
	/**
	 * <p>Creates a new file. The idea of this functions is to create a temporary file.
	 * <p>Extracted from: http://www.anddev.org/working_with_files-t115.html
	 * @param fileName The name of the temp file that will be created
	 * @param contents A string containing the contents to be written in the new file
	 * @param c The Activity context
	 */
	public static void createNewFile(String fileName, String contents, Context c) {
		//Writing a file...  
		try { 
		       /* We have to use the openFileOutput()-method
		       * the ActivityContext provides, to
		       * protect your file from others and
		       * This is done for security-reasons.
		       * We chose MODE_WORLD_READABLE, because
		       *  we have nothing to hide in our file */             
			   FileOutputStream fOut = c.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
		       OutputStreamWriter osw = new OutputStreamWriter(fOut); 
		       // Write the string to the file
		       osw.write(contents);
		       /* ensure that everything is
		        * really written out and close */
		       osw.flush();
		       osw.close();
		    } catch (IOException ioe) {
		    	ioe.printStackTrace();
		    }

	}
	
	/**
	 * <p>Open a temporary file and returns his contents
	 * <p>Extracted from: http://coderzheaven.com/2011/02/how-to-read-a-text-file-in-android-2/
	 * @param fileName The name of the file
	 * @param c The Activity content
	 * @return The content of the file
	 */
	public static String openFile(String fileName, Context c) {
		String res = "";
		try {
			InputStream in = c.openFileInput(fileName);
			// prepare the file for reading
			InputStreamReader input = new InputStreamReader(in);
			BufferedReader buffreader = new BufferedReader(input);
			res = "";
			String line;
			while ((line = buffreader.readLine()) != null) {
				res += line;
			}
			in.close();
		} catch (Exception e) {
			e.getMessage();
		}
		return res;
	}


}
